import javax.swing.text.StyledEditorKit;
import java.util.Arrays;
import java.util.Stack;
import java.util.concurrent.ForkJoinPool;

public class Main {

    public static void main(String[] args) {//不规则二维数组 可以指定每列的长度
        int[][] array=new int[2][];//二维数组定义可以省略列
        System.out.println(array[0]);//省略列时输出为null

        array[0]=new int[3];
        array[1]=new int[5];

        for(int i=0;i<array.length;i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

    }

    public static void main11(String[] args) {//二维数组本质是一维数组
        int[][]array={{1,2,3},{4,5,6}};//其中array[0]中存放的是一个一维数组的地址

        int[][]array1=new int[][]{{1,2,3},{4,5,6}};

        int[][]array2=new int[2][3];

        System.out.println(Arrays.deepToString(array));//deepToString可以直接打印二维数组

        System.out.println(Arrays.toString(array[0]));
        System.out.println(Arrays.toString(array[1]));

        for(int i=0;i<array.length;i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

            for (int[] t:array){
                for (int x:t){
                    System.out.print(x + " ");
                }
                System.out.println();
            }

    }

    public static int fun2(int... array){//int... array 可以接收多个值和数组
        int sum=0;
        for(int i=0;i<array.length;i++)
            sum+=array[i];
        return sum;
    }
    public static void main10(String[] args) {
        int[]array1=new int[]{1,2,3,4,5,6,7,8,9};
        System.out.println(fun2(1, 2, 3, 4, 5, 6, 7, 8, 9));
        System.out.println(fun2(array1));
    }


    public static void reverse(int[]array){
        int l=0,r=array.length-1;
        while(l<r){
            int t=array[l];
            array[l]=array[r];
            array[r]=t;
            l++;
            r--;
        }
    }

    public static void main9(String[] args) {//逆置
        int[]array1=new int[]{1,2,3,4,5,6,7,8,9};
        reverse(array1);
        System.out.println(Arrays.toString(array1));
    }



    public static void bubbleSort2(int[]array){
        int sum=0;
        for(int i=0;i<array.length-1;i++){
            for (int j=0;j<array.length - 1 - i;j++){
                sum++;
                if(array[j]>array[j+1]){
                    int t=array[j];
                    array[j]=array[j+1];
                    array[j+1]=t;
                }
            }
        }
        System.out.println(sum);
    }
    public static void bubbleSort1(int[]array){
        int sum=0;
        for(int i=0;i<array.length-1;i++){
            for (int j=i+1;j<array.length;j++){
                sum++;
                if(array[i]>array[j]){
                    int t=array[i];
                    array[i]=array[j];
                    array[j]=t;
                }
            }
        }
        System.out.println(sum);
    }
    public static void main8(String[] args) {//冒泡排序
        int[]array1=new int[]{1,3,4,6,7,9,8,5,2};
        int[]array2=new int[]{1,3,4,6,7,9,8,5,2};
        bubbleSort1(array1);
        bubbleSort2(array2);
    }
    public static void main7(String[] args) {//fill方法填充 可全可部分 局部时左闭右开
        int[]array=new int[]{1,2,3,4,5,6,7,8,9};
        Arrays.fill(array,-1);
        System.out.println(Arrays.toString(array));
    }

    public static void main6(String[] args) {//equals方法验证两个数组是否相同
        int[]array1=new int[]{1,2,3,4,5,6,7,8,9};
        int[]array2=new int[]{1,2,3,4,5,5,7,8,9};
        boolean flg =Arrays.equals(array1,array2);
        System.out.println(flg);
    }

    public static void main5(String[] args) {//sort方法排序
        int[]array=new int[]{1,3,4,6,7,9,8,5,2};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }

    public static int binarySearch(int[] array, int key){
        int l=0, r=array.length-1;
        while(l<=r){
            int mid=(l+r)/2;
            if(array[mid]>key){
                r=mid-1;
            } else if (array[mid]<key) {
                l=mid+1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main4(String[] args) {//binarySearch方法查找
        int[]array=new int[]{1,2,3,4,5,6,7,8,9};
        System.out.println(binarySearch(array,2));
        System.out.println(Arrays.binarySearch(array,8));
    }



    public static String ToString(int[] array){
        String s="[";
        for(int i=0;i<array.length-1;i++){
            s += array[i] + "，";
        }
        s+=array[array.length-1] + "]";
        return s;
    }

    public static void main3(String[] args) {//toString方法输出
        int[]array=new int[]{1,2,3,4};
        String res=ToString(array);
        System.out.println(res);
    }

    public static void main2(String[] args) {//数组引用
        int[] array =new int[]{10,20,30};

        func1(array);
        System.out.println(Arrays.toString(array));
        func2(array);
        System.out.println(Arrays.toString(array));
    }

    public static void func1(int[] array){
         array =new int[]{1,2,3};
    }

    public static void func2(int[] array){
        array[0]=199;
    }

    public static void main1(String[] args) {//数组引用
        int[] array1=new int[3] ;
        array1[0]=10;
        array1[1]=20;
        array1[2]=30;

        int[] array2=new int[]{1,2,3,4,5};
        array2[0]=100;
        array2[1]=200;
        array1=array2;

        array1[2]=300;
        array1[3]=400;
        array2[4]=500;

        for (int i=0;i< array1.length;i++) {
            System.out.print(array1[i] + " ");
        }
        System.out.println();
        for (int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");
        }

    }
}